#include "DbPart.h"
#include "connectpool.h"
#include <sstream>
#include <vector>
#include "Game.h"
#include "Logic.h"
#include "RedisClient.h"
#include "util.h"


char g_szSQL[2056]={0};
#define MAX_SQL_BUF_LEN (sizeof(g_szSQL)-1)

CDbPart::CDbPart()
{
	m_dbHandler = NULL;
}

CDbPart::~CDbPart()
{
}

bool CDbPart::Get(stAgencyEvent& agencyevent)
{
	do
	{
		std::stringstream sql;
		sql << "select agencyid, agencyname, call_url, currency, gamelist, ext, state from agencyevent where agencyid=" << agencyevent.agency_id << ";";
		bool ret = m_dbHandler->m_pStore->Query(sql.str().c_str());
		if (!ret)
		{
			log_error("ret is falied %d, %s",ret, sql.str().c_str());
			break;
		}
		log_debug("sql:%s", sql.str().c_str());
		unsigned long count = m_dbHandler->m_pStore->RowCount();
		if (count <= 0)
		{
			log_debug("count is %lu, %s",count, sql.str().c_str());
			break;
		}
		agencyevent.agency_id = (m_dbHandler->m_pStore->GetItemLong(0, "agencyid"));
		agencyevent.call_url = (m_dbHandler->m_pStore->GetItemString(0, "call_url"));
		agencyevent.currency = (m_dbHandler->m_pStore->GetItemString(0, "currency"));
		std::vector<std::string> gamelist_str_group;
		CTools::split_str(m_dbHandler->m_pStore->GetItemString(0, "gamelist").c_str(), ",", gamelist_str_group);
		for (auto item : gamelist_str_group)
		{
			agencyevent.gamelist.emplace(::strtoul(item.c_str(), NULL, 10));
		}
		agencyevent.ext = (m_dbHandler->m_pStore->GetItemString(0, "ext"));
		agencyevent.state = (m_dbHandler->m_pStore->GetItemLong(0, "state"));
		agencyevent.agency_name = (m_dbHandler->m_pStore->GetItemString(0, "agencyname"));
		
		log_debug("agencyevent:%s", agencyevent.ToString().c_str());
		return true;
	} while(false);
	return false;
}


bool CDbPart::GetCurrency(stCurrency& st_currency)
{
	do
	{
		std::stringstream sql;
		sql << "select unit_multi, label from currency where currency='" << st_currency.currency << "'";
		bool ret = m_dbHandler->m_pStore->Query(sql.str().c_str());
		if (!ret)
		{
			log_error("ret is falied %d, %s",ret, sql.str().c_str());
			break;
		}
		log_debug("sql:%s", sql.str().c_str());
		unsigned long count = m_dbHandler->m_pStore->RowCount();
		if (count <= 0)
		{
			log_error("count is %lu, %s",count, sql.str().c_str());
			break;
		}
		st_currency.unit_multi = (m_dbHandler->m_pStore->GetItemLongLong(0, "unit_multi"));
		st_currency.label = (m_dbHandler->m_pStore->GetItemString(0, "label"));
		return true;
	} while(false);
	return false;
}

bool CDbPart::GetAgencyId(stAgencyEvent& agencyevent)
{
	do
	{
		std::stringstream sql;
		sql << "select agencyid from agencyevent where agencyname='" << agencyevent.agency_name << "';";
		bool ret = m_dbHandler->m_pStore->Query(sql.str().c_str());
		if (!ret)
		{
			log_error("ret is falied %d, %s",ret, sql.str().c_str());
			break;
		}
		log_debug("sql:%s", sql.str().c_str());

		unsigned long count = m_dbHandler->m_pStore->RowCount();
		if (count <= 0)
		{
			log_debug("count is %lu, %s",count, sql.str().c_str());
			break;
		}

		agencyevent.agency_id = (m_dbHandler->m_pStore->GetItemLong(0, "agencyid"));
		log_debug("agencyevent:%s", agencyevent.ToString().c_str());
		return true;
	} while(false);
	return false;
}


